package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.domain.ChatRoomEntity;
import com.domain.ChatUserRelaEntity;
import com.dto.PageResult;
import com.mapper.ChatRoomMapper;
import com.mapper.ChatUserRelaMapper;
import com.service.ChatRoomService;
import com.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 聊天室房间表 服务实现类
 * </p>
 *
 * @author sly
 * @since 2023-12-30
 */
@Service
public class ChatRoomServiceImpl extends ServiceImpl<ChatRoomMapper, ChatRoomEntity> implements ChatRoomService {

    @Autowired
    private ChatRoomMapper chatRoomMapper;

    @Autowired
    private ChatUserRelaMapper chatUserRelaMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<ChatRoomEntity> pageListChat(ChatRoomEntity chat) {
        LambdaQueryWrapper<ChatRoomEntity> chatQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(chat.getChatTitle())) {
            chatQueryWrapper
                    .and(wq -> {
                        wq.like(ChatRoomEntity::getChatTitle, chat.getChatTitle());
                        wq.or().like(ChatRoomEntity::getChatIntroduction, chat.getChatTitle());
                        wq.or().like(ChatRoomEntity::getChatManagerName, chat.getChatTitle());
                    });
        }
        chatQueryWrapper
                .eq(ChatRoomEntity::getIsDel, 0)
                .eq(chat.getChatManagerId() != null, ChatRoomEntity::getChatManagerId, chat.getChatManagerId())
                .orderByDesc(ChatRoomEntity::getPeopleNum);

        // 分页查询
        Page<ChatRoomEntity> page = new Page<>(chat.getCurrentPage(), chat.getCurrentSize());
        IPage<ChatRoomEntity> pageResult = chatRoomMapper.selectPage(page, chatQueryWrapper);
        return new PageResult<>((int) pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    public ChatRoomEntity getChatDetail(Long chatId) {
        // 查询详情
        QueryWrapper<ChatRoomEntity> chatQueryWrapper = new QueryWrapper<>();
        chatQueryWrapper.lambda().eq(ChatRoomEntity::getId, chatId).eq(ChatRoomEntity::getIsDel, 0);
        ChatRoomEntity chat = chatRoomMapper.selectOne(chatQueryWrapper);
        return chat;
    }

    @Override
    public void addChat(ChatRoomEntity chat) {
        chat.setChatManagerId(1L);
        chat.setChatManagerName("admin");
        chat.setCreateTime(new Date());
        chat.setPeopleNum(1L);
        chat.setIsDel(0);
        chatRoomMapper.insert(chat);
        // 添加群主进群
        ChatUserRelaEntity chatUserRelaEntity = new ChatUserRelaEntity();
        chatUserRelaEntity.setChatId(chat.getId());
        chatUserRelaEntity.setUserId(chat.getChatManagerId());
        chatUserRelaEntity.setUserLastIn(new Date());
        chatUserRelaEntity.setCreateTime(new Date());
        chatUserRelaMapper.insert(chatUserRelaEntity);
    }

    @Override
    public void updateChat(ChatRoomEntity chat) {
        chatRoomMapper.updateById(chat);
    }

    @Override
    public void deleteChat(Long chatId) {
        ChatRoomEntity chatEntity = new ChatRoomEntity();
        chatEntity.setId(chatId);
        chatEntity.setIsDel(1);
        chatRoomMapper.updateById(chatEntity);
        // 删除聊天室的所有用户
        QueryWrapper<ChatUserRelaEntity> chatQueryWrapper = new QueryWrapper<>();
        chatQueryWrapper.lambda().eq(ChatUserRelaEntity::getChatId, chatId);
        chatUserRelaMapper.delete(chatQueryWrapper);
    }
}
